package com.junjie.blibli.api;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.junjie.blibli.Entity.FollowingGroup;
import com.junjie.blibli.Entity.UserMoment;
import com.junjie.blibli.Entity.Video;
import com.junjie.blibli.result.JsonResponse;
import com.junjie.blibli.service.DemoService;
import com.junjie.blibli.service.ElasticSearch.ElasticSearchService;
import com.junjie.blibli.util.FastDFSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.Email;
import javax.validation.constraints.Max;
import java.util.List;

@RestController
@RequestMapping("/demo")
@Validated
public class DemoApi {
    @Autowired
    private DemoService demoService;

    @Autowired
    private FastDFSUtil fastDFSUtil;

    @Autowired
    private ElasticSearchService elasticSearchService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //测试ES取出视频
    @GetMapping("/es-videos")
    public JsonResponse<Video> getEsVideos(@RequestParam String keyWord){
        Video videos = elasticSearchService.getVideos(keyWord);
        return new JsonResponse<>(videos);
    }

    @GetMapping("/query")
    public Long query(Long id){
        return demoService.query(id);
    }

    @GetMapping("/slices")
    public void slices(MultipartFile file) throws Exception {
        fastDFSUtil.convertFileToSlices(file);
    }

    @GetMapping("/testValid/{email}")
    public void testValid(@PathVariable @Email String email){
        System.out.println("ok");
        System.out.println("email为"+email);
    }

    //获取用户关注分组
    @GetMapping("/user-following-groups")
    private JsonResponse<List<Object>> getRedisAnything(String redisKey){
        String result = redisTemplate.opsForValue().get(redisKey);
        List<Object> list = JSONArray.parseArray(result, Object.class);
        return new JsonResponse<>(list);
    }
}
